<template>
  <div class="mb-4">
    <el-row>
      <el-col :span="6">
        <research5 @output="onput" />
      </el-col>
      <el-col :span="2">
        <el-button type="primary" plain @click="onClick">搜索</el-button>
      </el-col>
      <el-col :span="3">
        <el-button plain @click="reSet">重置</el-button>
      </el-col>
    </el-row>
  </div>
  <br />
  <br />
  <el-table
    :data="tableData"
    border
    style="width: 100%"
    @selection-change="handleSelectionChange"
  >
    <el-table-column type="selection" width="55" />
    <el-table-column prop="link" label="链接" width="180" />
    <el-table-column prop="title" label="标题" width="180" />
    <el-table-column prop="time" label="新闻时间" width="180" />
    <el-table-column prop="commentNum" label="评论数" width="250" />
    <el-table-column prop="content" label="新闻内容" width="250" />
  </el-table>
</template>

<script lang="ts">
import research5 from './research5.vue';
export default {
  components: {
    research5
  }
}
</script>

<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import axios from 'axios';

const input = ref(0);
const tableData = ref([]);
const selectedRows = ref([]);

const onput = (e) => {
  console.log(e);
  input.value = e;
}

const onClick = () => {
  try {
    axios.get('http://localhost:9001/UserAccount/searchUser', {
      params: { info: input.value }
    }).then(response => {
      console.log(input);
      tableData.value = response.data;
    });
  } catch (error) {
    console.error('连接失败：', error.message);
  }
}

const reSet = () => {
  input.value = "";
  try {
    axios.get('http://localhost:9001/UserAccount/getAllUser', {
      params: { info: input.value }
    }).then(response => {
      console.log(input);
      tableData.value = response.data;
    });
  } catch (error) {
    console.error('连接失败：', error.message);
  }
}

const handleSelectionChange = (val) => {
  selectedRows.value = val;
}

const deleteSelected = () => {
  const idsToDelete = selectedRows.value.map(row => row.id);
  // 在这里调用后端API删除选中的用户
  try {
    axios.post('http://localhost:9001/UserAccount/deleteUsers', idsToDelete)
      .then(response => {
        console.log('删除成功');
        // 更新表格数据
        tableData.value = tableData.value.filter(user => !idsToDelete.includes(user.id));
      });
  } catch (error) {
    console.error('删除失败：', error.message);
  }
}

onMounted(async () => {
  try {
    const response = await axios.get('http://localhost:9001/UserAccount/getAllUser');
    tableData.value = response.data;
  } catch (error) {
    console.error('连接失败：', error.message);
  }
});
</script>
